#include "ExTexture2D.h"
using namespace affine;
using namespace cocos2d;

void ExTexture2D::Draw() const
{
	GLfloat	 coordinates[] = {
		0,		0,
		m_tex->getMaxS(),	0,
		0,		m_tex->getMaxT(),
		m_tex->getMaxS(),	m_tex->getMaxT()
	};
	affine::affine_point2d_t p[4];
	p[0] = 0, m_tex->getContentSizeInPixels().height, 1;
	p[1] = m_tex->getContentSizeInPixels().width, m_tex->getContentSizeInPixels().height, 1;
	p[2] = 0, 0, 1;
	p[3] = m_tex->getContentSizeInPixels().width, 0, 1;
	p[0] = m_affine * p[0];
	p[1] = m_affine * p[1];
	p[2] = m_affine * p[2];
	p[3] = m_affine * p[3];
	
	
	GLfloat	vertices[] = {
		(p[0])[0], (p[0])[1],
		(p[1])[0], (p[1])[1],
		(p[2])[0], (p[2])[1],
		(p[3])[0], (p[3])[1]
	};
	
	glBindTexture(GL_TEXTURE_2D, m_tex->getName());
	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void ExTexture2D::Draw(float alpha)
{
	GLfloat	 coordinates[] = {
		0,		0,
		m_tex->getMaxS(),	0,
		0,		m_tex->getMaxT(),
		m_tex->getMaxS(),	m_tex->getMaxT()
	};
	affine::affine_point2d_t p[4];
	p[0] = 0, m_tex->getContentSizeInPixels().height, 1;
	p[1] = m_tex->getContentSizeInPixels().width, m_tex->getContentSizeInPixels().height, 1;
	p[2] = 0, 0, 1;
	p[3] = m_tex->getContentSizeInPixels().width, 0, 1;
	p[0] = m_affine * p[0];
	p[1] = m_affine * p[1];
	p[2] = m_affine * p[2];
	p[3] = m_affine * p[3];
	
	
	GLfloat	vertices[] = {
		(p[0])[0], (p[0])[1],
		(p[1])[0], (p[1])[1],
		(p[2])[0], (p[2])[1],
		(p[3])[0], (p[3])[1]
	};
	
	glBindTexture(GL_TEXTURE_2D, m_tex->getName());
	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void ExTexture2D::DrawWithTexRotate90() const
{
	GLfloat	 coordinates[] = {
		m_tex->getMaxS(),	0,
		m_tex->getMaxS(),	m_tex->getMaxT(),
		0,		0,
		
		0,		m_tex->getMaxT(),
		
	};
	affine::affine_point2d_t p[4];
	p[0] = 0, m_tex->getContentSizeInPixels().height, 1;
	p[1] = m_tex->getContentSizeInPixels().width, m_tex->getContentSizeInPixels().height, 1;
	p[2] = 0, 0, 1;
	p[3] = m_tex->getContentSizeInPixels().width, 0, 1;
	p[0] = m_affine * p[0];
	p[1] = m_affine * p[1];
	p[2] = m_affine * p[2];
	p[3] = m_affine * p[3];
	
	
	GLfloat	vertices[] = {
		(p[0])[0], (p[0])[1],
		(p[1])[0], (p[1])[1],
		(p[2])[0], (p[2])[1],
		(p[3])[0], (p[3])[1]
	};
	
	glBindTexture(GL_TEXTURE_2D, m_tex->getName());
	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

//without drawing
void ExTexture2D::MoveToRect(CCRect rect)
{
	Indentity();
	float scale;
	assert(FLOAT_IS_NOT_ZERO(rect.size.width) && FLOAT_IS_NOT_ZERO(rect.size.height));
	float w_h = rect.size.width / rect.size.height;
	float origin_w_h = m_tex->getContentSizeInPixels().width / m_tex->getContentSizeInPixels().height;
	if (w_h  >  origin_w_h) {
		scale = rect.size.height / m_tex->getContentSizeInPixels().height;
		Scale(scale);
		Translate(rect.origin.x + (rect.size.width - m_tex->getContentSizeInPixels().width*scale)/2, rect.origin.y);
	}
	else {
		scale = rect.size.width / m_tex->getContentSizeInPixels().width;
		Scale(scale);
		Translate(rect.origin.x, rect.origin.y + (rect.size.height - m_tex->getContentSizeInPixels().height*scale)/2);
	}
}